package com.lzl.utils.fastjson2;

import com.alibaba.fastjson2.filter.ValueFilter;

import java.sql.Date;
import java.text.SimpleDateFormat;
import java.util.LinkedHashMap;
import java.util.Set;

public class FastJson2DateValueFilter implements ValueFilter {
    public FastJson2DateValueFilter(LinkedHashMap<String,String> dateMappings){
        this.dateMappings = dateMappings;
    }

    private LinkedHashMap<String,String> dateMappings = null;
    @Override
    public Object apply(Object object, String propertyName, Object value) {
        if(dateMappings == null){
            return value;
        }
        if(value instanceof Date){
            Set<String> keySet = dateMappings.keySet();
            for(String pattern : keySet){
                if(propertyName.matches(pattern)){
                    String dateFormat = dateMappings.get(pattern);
                    SimpleDateFormat formatter = new SimpleDateFormat(dateFormat);
                    return formatter.format((Date)value);
                }
            }
        }
        return value;
    }
}
